﻿using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace FileCopyExtensions
{
    public partial class UI_FileCopyExtensions : UserControl
    {
        // declaration ---------------------------------------------------------------------------------------

        CopyFileCallbackAction copyAction;
        //private static string progressBarNormal = "#FF01D328";
        //private static string progressBarCancel = 
        private static Color pgrNormal;
        private static Color pgrCancel = Color.Yellow;
        private static Color pgrStop = Color.Gray;
        private static Color pgrErreur = Color.Red;
        private static Color pgrSuccess = Color.Blue;

        CopyResult copyState;
        public CopyResult CopyState
        {
            get { return copyState; }
            set { copyState = value; }
        }

        bool vertical;

        [Category("Configuration"), Browsable(true), Description("Set the disposition vertical or hoirizontal")]
        public bool Vertical
        {
            get {return vertical; }
            set
            {
                vertical = value;
                if (vertical)
                {
                    lbl_pourcentage.AutoSize = false;
                    lbl_pourcentage.Dock = DockStyle.Bottom;
                }
                else
                {
                    lbl_pourcentage.AutoSize = true;
                    lbl_pourcentage.Dock = DockStyle.Right;
                    
                }
            }
        }

        bool showProgress;

        [Category("Configuration"), Browsable(true), Description("Set the progress label visible or not")]
        public bool ShowProgress
        {
            get { return showProgress; }
            set { 
                showProgress = value;
                if (showProgress)
                {
                    lbl_pourcentage.Visible = true;
                }
                else
                    lbl_pourcentage.Visible = false;
            }
        }

        FileInfo ficSource, folderDestination;

        public FileInfo FolderDestination
        {
            get { return folderDestination; }
            set { folderDestination = value; }
        }

        public FileInfo FileSource
        {
            get { return ficSource; }
            set { ficSource = value; }
        }

        private event CopyFileCallback CopyProgress;
        private event CopyFileTerminate CopyEnd;

        // Delegates -----------------------------------------------------------------------------------------
        public delegate void MiseAJourProgress_delegate(double value);

        // Enum ----------------------------------------------------------------------------------------------

        // Fonctions ------------------------------------------------------------------------------------------
        
        public UI_FileCopyExtensions()
        {
            InitializeComponent();
            
            Vertical = true;
            ShowProgress = true;
            FileSource = null;
            FolderDestination = null;

            string colorHexa = "#FF01D328";
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(Color));
            pgrNormal = (Color)converter.ConvertFrom(colorHexa);
        }

        /// <summary>
        /// Lance la copie des fichiers
        /// </summary>
        /// <param name="CopyOption">The operation to perform when starting copying</param>
        public void runCopy(CopyFileOptions CopyOption = CopyFileOptions.All)
        {
            copyAction = CopyFileCallbackAction.Continue;
            pgr.ForeColor = pgrNormal;
            CopyProgress += new CopyFileCallback(CopyProgress_Handler);
            CopyEnd +=new CopyFileTerminate(CopyEnd_Handler);
            FileCopyExtensions.CopyFile(FileSource, new FileInfo(FolderDestination.FullName + "\\" + FileSource.Name), CopyOption, CopyProgress, CopyEnd);
        }

        /// <summary>
        /// Cancel the copy in progress.
        /// The copy is stop and can't be restart
        /// </summary>
        public void cancelCopy()
        {
            copyAction = CopyFileCallbackAction.Cancel;
        }

        /// <summary>
        /// Stop the copy. Restart is possible.
        /// </summary>
        public void stopCopy()
        {
            copyAction = CopyFileCallbackAction.Stop;
        }


        /// <summary>
        /// Handle soulevé à chaque pas de progression de la copie
        /// </summary>
        /// <param name="source">Fichier source</param>
        /// <param name="destination">Dossier de destination</param>
        /// <param name="state">Etat</param>
        /// <param name="totalFileSize">taille du fichier</param>
        /// <param name="totalBytesTransferred">nombre de bits transféré</param>
        /// <returns></returns>
        private CopyFileCallbackAction CopyProgress_Handler(FileInfo source, FileInfo destination, object state, long totalFileSize, long totalBytesTransferred)
        {
            //progressBar1.Value = (double)totalBytesTransferred / (double)totalFileSize * 100;
           
            this.BeginInvoke(new MiseAJourProgress_delegate(MiseAJourProgress), (double)totalBytesTransferred / (double)totalFileSize * 100);
            //Console.WriteLine("state = " + state);

            return copyAction;
        }

        /// <summary>
        /// Handle soulevé à la fin de la copie
        /// </summary>
        /// <param name="success"></param>
        private void CopyEnd_Handler(bool success)
        {
            if (success)
            {
                pgr.ForeColor = pgrSuccess;
                CopyState = CopyResult.CopyTerminateOk;
            }
            else if (copyAction == CopyFileCallbackAction.Cancel)
            {
                pgr.ForeColor = pgrCancel;
                CopyState = CopyResult.CopyCancel;
            }
            else if (copyAction == CopyFileCallbackAction.Stop)
            {
                pgr.ForeColor = pgrStop;
                CopyState = CopyResult.CopyStop;
            }
            else
            {
                pgr.ForeColor = pgrErreur;
                CopyState = CopyResult.CopyError;
            }
        }

        /// <summary>
        /// Mise à jour de l'affichage
        /// </summary>
        /// <param name="value"></param>
        private void MiseAJourProgress(double value)
        {
            pgr.Value = (int)value;
            lbl_pourcentage.Text = value.ToString("F2")+" %";
        }
    }
}
